package components
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * @author perry
	 */
	public class Btn extends BtnView
	{
		public static const NAME:String 			= "Btn";
		public static const CLICKED:String = NAME + "Clicked";
		
		private var btnData:Object;

		public function Btn()
		{
		}
		
		public function init( d:Object ):void
		{
			btnData = d;
			
			buildBtn();
			addEvents();
		}
		
		public function select():void
		{
			this.gotoAndStop( 2 );
			removeEvents();
		}
		
		public function unselect():void
		{
			this.gotoAndStop( 1 );
			addEvents();
		}
		
		
		public function addEvents():void
		{
			cover.buttonMode = true;
			cover.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			cover.addEventListener( MouseEvent.MOUSE_OVER, mouseOverHandler );
			cover.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
		}
		
		public function removeEvents():void
		{
			cover.buttonMode = false;
			cover.removeEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			cover.removeEventListener( MouseEvent.MOUSE_OVER, mouseOverHandler );
			cover.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);	
		}

		private function buildBtn():void
		{
			label.text = btnData.info;
		}

		private function mouseDownHandler( e:MouseEvent ):void
		{
			dispatchEvent( new Event( CLICKED ) );
		}

		private function mouseOverHandler( e:MouseEvent ):void
		{
			this.gotoAndStop( 2 );
		}

		private function mouseOutHandler( e:MouseEvent ):void
		{
			this.gotoAndStop( 1 );
		}
		
		public function get data():Object
		{
			return btnData;
		}

	}
}
